package com.wsy.service;

import com.wsy.dao.CustomerDao;
import com.wsy.entity.Customer;

import com.wsy.entity.Line;
import com.wsy.mapper.CustomerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class CustomerService {

//    @Autowired
//    CustomerDao customerDao;

    @Autowired
//    CustomerMapper customerMapper;
    CustomerMapper customerMapper;

    public Customer register(Customer customer) {
        Customer registerCus = customerMapper.getUserByAccount(customer.getAccount());
        if (registerCus == null) {
            System.out.println(customer);
            customerMapper.saveUser(customer);
            return customer;
        } else {
            return null;
        }
    }
//用户登录验证
    public Customer login(Customer user) {
        System.out.println(user.getAccount());
        Customer loginUser = customerMapper.getUserByAccount(user.getAccount());
        System.out.println(loginUser);
        if (loginUser == null) {
            return null;
        } else {
            System.out.println(user);
            if (user.getPassword() != null && user.getPassword().equals(loginUser.getPassword()) && loginUser.getType() == 0) {
                String token = UUID.randomUUID().toString();
                loginUser.setToken(token);
                customerMapper.setTokenToUser(token, loginUser.getCustomerID());
                return loginUser;
            }
        }
        return null;
    }
//管理员登录验证
    public Customer adminLogin(Customer user) {
        Customer loginUser = customerMapper.getUserByAccount(user.getAccount());
        if (loginUser == null) {
            return null;
        } else {
            if (user.getPassword() != null && user.getPassword().equals(loginUser.getPassword()) && loginUser.getType() == 1) {
                String token = UUID.randomUUID().toString();
                loginUser.setToken(token);
                customerMapper.setTokenToUser(token, loginUser.getCustomerID());
                return loginUser;
            }
        }

        return null;
    }

//    购物车数据
    public List<Line> getCat(Integer customerID){
        return customerMapper.getCat(customerID);
    }
    //    购物车数据
    public List<Line> getOrder(Integer odID){
        return customerMapper.getOrder(odID);
    }

//    添加订单
    public int postOrder(Line line){
        System.out.println(line);
        return customerMapper.postOrder(line);
    }

//    删除购物车
    public int delCat(int id){
//        System.out.println(line);
        return  customerMapper.delCat(id);
    }

    public Line getToken(String token) {
        return customerMapper.gettoken(token);
    }

    public  List getNum() {return customerMapper.getNum();}

    public  Line getjie() {return customerMapper.getjie();}

    public int putNum(int num){return customerMapper.putNum(num);}

    public int postAddcar(String token){
        Line list =customerMapper.gettoken(token);
        return customerMapper.postAddcar(list.getCustomerID(),25);}
}
